import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:just_audio/just_audio.dart';
// 确保pubspec.yaml中已添加get和just_audio依赖
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:simple_music_app/pages/player_page/components/music_control.dart';

class FloatMusicPlayer extends StatefulWidget {
  const FloatMusicPlayer({super.key});

  @override
  State<FloatMusicPlayer> createState() => _FloatMusicPlayerState();
}

class _FloatMusicPlayerState extends State<FloatMusicPlayer> {
  final MusicController audioController = Get.put(MusicController());

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      elevation: 8.0,
      child: GestureDetector(
        onTap: () {
          // Get.toNamed(AppRoutes.player);
        },
        child: Container(
          width: MediaQuery.of(context).size.width * 0.95,
          padding:
              const EdgeInsets.only(left: 4, right: 4, top: 5, bottom: 5).r,
          decoration: BoxDecoration(
            color: Colors.black,
            borderRadius: BorderRadius.circular(10).r,
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              ClipPath(
                clipper: ShapeBorderClipper(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0).r,
                  ),
                ),
                child: Obx(() {
                  final playlist = audioController.storePlaylist;
                  final currentIndex = audioController.currentIndex.value;

                  if (playlist.isEmpty || currentIndex >= playlist.length) {
                    return Container(
                      height: 50.w,
                      width: 50.w,
                      color: Colors.grey[800],
                      child: const Icon(Icons.music_note, color: Colors.white),
                    );
                  }

                  return CachedNetworkImage(
                    imageUrl: playlist[currentIndex].coverUrl,
                    height: 50.w,
                    width: 50.w,
                    fit: BoxFit.cover,
                    errorWidget: (context, url, error) => Container(
                      height: 50.w,
                      width: 50.w,
                      color: Colors.grey[800],
                      child: const Icon(Icons.music_note, color: Colors.white),
                    ),
                  );
                }),
              ),
              Obx(() {
                final playlist = audioController.storePlaylist;
                final currentIndex = audioController.currentIndex.value;

                return Expanded(
                  flex: 2,
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.w),
                    child: Text(
                      playlist.isEmpty || currentIndex >= playlist.length
                          ? '没有正在播放的歌曲'
                          : '正在播放：${playlist[currentIndex].songName}',
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(fontSize: 12.sp),
                    ),
                  ),
                );
              }),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  StreamBuilder<SequenceState?>(
                    stream: audioController.audioPlayer.sequenceStateStream,
                    builder: (context, snapshot) => IconButton(
                      icon: Icon(
                        Icons.fast_rewind_rounded,
                        size: 24.sp,
                      ),
                      onPressed: () {
                        audioController.seekToPrevious();
                      },
                    ),
                  ),
                  StreamBuilder<PlayerState>(
                    stream: audioController.audioPlayer.playerStateStream,
                    builder: (context, snapshot) {
                      final playerState = snapshot.data;
                      final processingState = playerState?.processingState;
                      final playing = playerState?.playing;
                      if (processingState == ProcessingState.loading ||
                          processingState == ProcessingState.buffering) {
                        return SizedBox(
                          width: 24.0.sp,
                          height: 24.0.sp,
                          child: const CircularProgressIndicator(),
                        );
                      } else if (playing != true) {
                        return IconButton(
                          icon: const Icon(Icons.play_arrow_rounded),
                          onPressed: audioController.resume,
                        );
                      } else if (processingState != ProcessingState.completed) {
                        return IconButton(
                          icon: const Icon(Icons.pause),
                          onPressed: audioController.pause,
                        );
                      } else {
                        return IconButton(
                          icon: const Icon(Icons.replay),
                          onPressed: () => audioController.seek(Duration.zero),
                        );
                      }
                    },
                  ),
                  StreamBuilder<SequenceState?>(
                    stream: audioController.audioPlayer.sequenceStateStream,
                    builder: (context, snapshot) => IconButton(
                      icon: Icon(
                        Icons.fast_forward_rounded,
                        size: 24.sp,
                      ),
                      onPressed: () {
                        audioController.seekToNext();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
